﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace MSysMonNET
{
    /// <summary>
    /// This class encapsulates the methods for monitoring database engine availability
    /// </summary>
    public class MSysCheckDatabases
    {
        /// <summary>
        /// The method checks if a connection to a given SQL Server is possible
        /// </summary>
        /// <param name="connectionString">The connection string for the desired SQL Server</param>
        /// <returns>an instance of the MSysServiceStatus with the test's result</returns>
        public MSysServiceStatus checkSQLServerConnection(string connectionString)
        {
            MSysServiceStatus clsReturn = new MSysServiceStatus();
            SqlConnection clsCon = new SqlConnection(connectionString);

            try
            {
                clsCon.Open();

                clsReturn.serviceStatus = true;
                clsReturn.returnCode=0;
                clsReturn.returnMessage="success";

                clsCon.Close();
            }
            catch (Exception ex)
            {
                clsReturn.serviceStatus = false;
                clsReturn.returnCode = -1;
                clsReturn.returnMessage = ex.Message;
            }

            return clsReturn;
        }
        /// <summary>
        /// The method checks if a connection to a given Database Engine is possible,
        /// using a managed OleDB provider. This requires for the driver to be installed.
        /// </summary>
        /// <param name="connectionString">The connection string for the desired Database Engine</param>
        /// <returns>an instance of the MSysServiceStatus with the test's result</returns>
        public MSysServiceStatus checkOleDBConnection(string connectionString)
        {
            MSysServiceStatus clsReturn = new MSysServiceStatus();
            OleDbConnection clsCon = new OleDbConnection(connectionString);

            try
            {
                clsCon.Open();

                clsReturn.serviceStatus = true;
                clsReturn.returnCode = 0;
                clsReturn.returnMessage = "success";

                clsCon.Close();
            }
            catch (Exception ex)
            {
                clsReturn.serviceStatus = false;
                clsReturn.returnCode = -1;
                clsReturn.returnMessage = ex.Message;
            }

            return clsReturn;
        }
    }
}
